#!/bin/sh

CSS=`sed 's/^\s*//;s/\s*$//;s/\s\+{/{/;s/\s*\([:|+|>|,]\)\s*/\1/' settings.css | tr -d '\n' | sed 's/[\/&]/\\\&/g;'"s/'/"'\\\\\\\\'"'/g;s/;}/}/g"`
HTML=`sed 's/^\s*//;s/\s*$//' settings.html | tr -d '\n' | sed 's/[\/&]/\\\&/g;'"s/'/"'\\\\\\\\'"'/g"`
VER=`sed -n '\|^// @version\s\+\([^\s]\+\).*$|{s//\1/;p;q;}' weiboFilter.user.js`
REV=`sed -n '\|^// @revision\s\+\([0-9]\+\).*$|{s//\1/;p;q;}' weiboFilter.user.js`
OUTFILE="weiboFilter-v${VER}.user.js"
while getopts ":no:" optname
do
	case "$optname" in
		"n") NO_COMPRESS=1;;
		"o") OUTFILE=$OPTARG;;
		"?") echo "Unknown option -$OPTARG"; exit 1;;
		":") echo "Missing argument value for option -$OPTARG"; exit 1;;
	esac
done
sed 's/\${CSS}/'"${CSS}"'/;s/\${HTML}/'"${HTML}"'/;s/\${REV}/'"${REV}"'/;s/\${VER}/'"${VER}"'/;s/^\(\/\/ @version\s\+\)\([0-9]\+\)\(\(\.[0-9]\+\)\+\).*$/\1\2\3/' weiboFilter.user.js > $OUTFILE
[ $NO_COMPRESS ] && exit 0
sed -i 's/\(console\..*\?);$\)/\/\*\1\*\//' $OUTFILE
head -n 19 $OUTFILE > weiboFilter.user.head.js
if [ ! -f compiler.jar ]
then
	wget -q http://closure-compiler.googlecode.com/files/compiler-latest.tar.gz
	tar -xzf compiler-latest.tar.gz compiler.jar
	rm -f compiler-latest.tar.gz
fi
java -jar compiler.jar --charset=UTF-8 --compilation_level SIMPLE_OPTIMIZATIONS --js=$OUTFILE --js_output_file=weiboFilter.user.body.js
cat weiboFilter.user.head.js weiboFilter.user.body.js > $OUTFILE
rm -f weiboFilter.user.head.js weiboFilter.user.body.js
